#include <sys_config.h>
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>

#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"

#include "string.id"
#include "images.id"
#include "osd_config.h"
#include "osd_rsc.h"

#include "copper_common/system_data.h"
#include "copper_common/menu_api.h"
#include "win_com_menu_define.h"
#include "win_com.h"
#include "win_com_popup.h"
#if 0

/*******************************************************************************
*	Objects definition
*******************************************************************************/
#if(defined POLAND_SPEC_SUPPORT)

char aus_time_pat_time[] = "t1";


#define SCAN_STANDBY_ID  1
#define SCAN_OPERATION_ID  2
#define SCAN_TIME_ID  3

extern CONTAINER g_win_automatic_upgrade_setting;

extern CONTAINER aus_item_con1;
extern CONTAINER aus_item_con2;
extern CONTAINER aus_item_con3;

extern TEXT_FIELD aus_item_txtname1;
extern TEXT_FIELD aus_item_txtname2;
extern TEXT_FIELD aus_item_txtname3;


extern TEXT_FIELD aus_item_txtset1;
extern TEXT_FIELD aus_item_txtset2;
extern EDIT_FIELD aus_item_edit1;


extern TEXT_FIELD aus_item_line1;
extern TEXT_FIELD aus_item_line2;
extern TEXT_FIELD aus_item_line3;

static VACTION aus_item_con_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT aus_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION aus_con_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT aus_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);
VACTION win_aus_edit_keymap(POBJECT_HEAD pObj, UINT32 Key);


#define LDEF_CON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
    aus_item_con_keymap,aus_item_con_callback,  \
    conobj, focusID,1)

#define LDEF_TXT(root,varTxt,nxtObj,l,t,w,h,resID)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, TXT_SH_IDX,TXT_HL_IDX,TXT_SL_IDX,TXT_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,NULL)

#define LDEF_TXTNAME(root,varTxt,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, TXTN_SH_IDX,TXTN_HL_IDX,TXTN_SL_IDX,TXTN_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,str)

#define LDEF_TXTSET(root,varTxt,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, TXTS_SH_IDX,TXTS_HL_IDX,TXTS_SL_IDX,TXTS_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_CENTER| C_ALIGN_VCENTER, 20,0,resID,str)

#define LDEF_EDIT(root, varNum, nxtObj, ID, IDl, IDr, IDu, IDd, l, t, w, h,style,cursormode,pat,pre,sub,str)	\
    DEF_EDITFIELD(varNum,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, TXTS_SH_IDX,WSTL_BUTTON_05_HD,TXTS_SL_IDX,TXTS_GRY_IDX,   \
    win_aus_edit_keymap,NULL,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 20,0,style,pat,10,cursormode,pre,sub,str)

#define LDEF_MM_ITEM(root,varCon,nxtObj,varTxtName,varTxtset,varLine,ID,IDu,IDd,l,t,w,h,nameID,setstr)	\
	LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxtName,1)	\
	LDEF_TXTNAME(&varCon,varTxtName,&varTxtset,0,0,0,0,0,l + TXTN_L_OF, t + TXTN_T_OF,TXTN_W,TXTN_H,nameID,NULL)	\
	LDEF_TXTSET(&varCon,varTxtset,NULL/*&varLine*/,1,1,1,1,1,l + TXTS_L_OF, t + TXTS_T_OF,TXTS_W,TXTS_H,0,setstr)\
	LDEF_LINE(&varCon,varLine,NULL,1,1,1,1,1,l + LINE_L_OF, t + LINE_T_OF,LINE_W,LINE_H,0,0)

#define LDEF_MM_ITEM2(root,varCon,nxtObj,varTxtName,varTxtset,varLine,ID,IDu,IDd,l,t,w,h,nameID,setstr,pat)	\
	LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxtName,1)	\
	LDEF_TXTNAME(&varCon,varTxtName,&varTxtset,0,0,0,0,0,l + TXTN_L_OF, t + TXTN_T_OF,TXTN_W,TXTN_H,nameID,NULL)	\
   	LDEF_EDIT(&varCon,varTxtset,NULL/*&varLine*/,1,1,1,1,1,l + TXTS_L_OF, t + TXTS_T_OF,TXTS_W,TXTS_H,NORMAL_EDIT_MODE , CURSOR_NORMAL, pat,NULL,NULL, setstr)\
	LDEF_LINE(&varCon,varLine,NULL,1,1,1,1,1,l + LINE_L_OF, t + LINE_T_OF,LINE_W,LINE_H,0,0)

#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
    	DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, WIN_SH_IDX,WIN_HL_IDX,WIN_SL_IDX,WIN_GRY_IDX,   \
    aus_con_keymap,aus_con_callback,  nxtObj, focusID,0)

#define WIN  g_win_automatic_upgrade_setting

			
LDEF_MM_ITEM(g_win_automatic_upgrade_setting,aus_item_con1, &aus_item_con2, aus_item_txtname1,aus_item_txtset1,aus_item_line1,1,3,2,	\
				CON_L, CON_T + (CON_H + CON_GAP)*0,CON_W,CON_H,RS_IN_STANDBY_MODE,NULL)
				
LDEF_MM_ITEM(g_win_automatic_upgrade_setting, aus_item_con2, & aus_item_con3, aus_item_txtname2,  aus_item_txtset2, aus_item_line2,2,1,3,	\
				CON_L, CON_T + (CON_H + CON_GAP)*1,CON_W,CON_H,RS_IN_OPERATION_MODE, NULL)
LDEF_MM_ITEM2(g_win_automatic_upgrade_setting, aus_item_con3, NULL, aus_item_txtname3, aus_item_edit1, aus_item_line3,3,2,1,	\
				CON_L, CON_T + (CON_H + CON_GAP)*2,CON_W,CON_H,RS_SYSTEM_TIME,display_strs[1], aus_time_pat_time)

LDEF_WIN(WIN,&aus_item_con1,W_L, W_T, W_W, W_H,1)

/////////////////////////////////////////////////////////////////////////////////

void win_aus_set_Channel_Upgrade_Standby_enable_string(UINT8 input)
{
	if(input==0)
		OSD_SetTextFieldContent(&aus_item_txtset1, STRING_ID,RS_COMMON_OFF);				
	else
		OSD_SetTextFieldContent(&aus_item_txtset1, STRING_ID,RS_COMMON_ON);	

}

void win_aus_set_Channel_Upgrade_Operation_enable_string(UINT8 input)
{
	if(input==0)
		OSD_SetTextFieldContent(&aus_item_txtset2, STRING_ID,RS_COMMON_OFF);				
	else
		OSD_SetTextFieldContent(&aus_item_txtset2, STRING_ID,RS_COMMON_ON);	
}

void get_automatic_channel_upgrade_time_input(UINT8* hour, UINT8* min)
{
	date_time dt;
	OSD_GetEditFieldTimeDate(&aus_item_edit1,&dt);
	*hour = dt.hour;
	*min =  dt.min;
}

void set_automatic_channel_upgrade_time(UINT8 hour, UINT8 min)
{
	OSD_SetEditFieldContent(&aus_item_edit1, STRING_NUMBER, hour*100+min);
}


static UINT8 check_is_valid_hh_mm(UINT8 hour, UINT8 min)
{
    UINT8 hh;
    UINT8 mm;

    hh=hour;
    mm=min;
    if(mm>=60 || hh>23)
        return 0;
    else
        return 1;
}


VACTION win_aus_edit_keymap(POBJECT_HEAD pObj, UINT32 Key)
{
	VACTION Action = VACT_PASS;

	switch(Key)
	{
		case V_KEY_LEFT:
			Action = VACT_EDIT_LEFT;
			break;
		case V_KEY_RIGHT:
			Action = VACT_EDIT_RIGHT;
			break;
		case V_KEY_0:	case V_KEY_1:	case V_KEY_2:	case V_KEY_3:
		case V_KEY_4:	case V_KEY_5:	case V_KEY_6:	case V_KEY_7:
		case V_KEY_8:	case V_KEY_9:
			Action = Key - V_KEY_0 + VACT_NUM_0;
			break;
		default:
			break;
	}
	
    return Action;
}

/*******************************************************************************
*	key mapping and event callback definition
*******************************************************************************/

static VACTION aus_item_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	
	switch(key)
	{
	case V_KEY_RIGHT:
		act = VACT_INCREASE;
		break;		
	case V_KEY_LEFT:		
		act = VACT_DECREASE;
		break;		
	case V_KEY_ENTER:
		act = VACT_ENTER;
		break;	
	default:	
		act = VACT_PASS;
		break;
	}

	return act;

}



void win_aus_left_right_key(POBJECT_HEAD pObj,UINT8 id)
{
	INT32 hoffset, moffset;
	AUTOSCAN_TIME* as_time = sys_data_get_autoscan_upgrade_info();

	switch(id)
	{
		
		case SCAN_STANDBY_ID:
			as_time->mode_stby=1-as_time->mode_stby;
			win_aus_set_Channel_Upgrade_Standby_enable_string(as_time->mode_stby);
			OSD_TrackObject(pObj,C_UPDATE_ALL);
			break;
		case SCAN_OPERATION_ID:
			as_time->mode_oper=1-as_time->mode_oper;
			win_aus_set_Channel_Upgrade_Operation_enable_string(as_time->mode_oper);
			OSD_TrackObject(pObj,C_UPDATE_ALL);
			break;			
	}
}


static PRESULT aus_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT8	unact,input;
	UINT8	id = OSD_GetObjID(pObj);
	UINT8 save;
	UINT8 hour, min;
		
	switch(event)
	{		
	case EVN_FOCUS_PRE_GET:
		break;
		
	case EVN_FOCUS_PRE_LOSE:
		break;
	
	case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1>>16) ;
		 if(unact == VACT_DECREASE)
		{
			win_aus_left_right_key(pObj,id);
		}
		else if(unact == VACT_INCREASE)
		{
			win_aus_left_right_key(pObj,id);
		}
		ret = PROC_LOOP;
		break;
	case EVN_KEY_GOT:
		ret = PROC_PASS;
		break;
	}
	return ret;

}

static VACTION aus_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	SYSTEM_DATA* p_sys_data=sys_data_get();
	date_time dt;
	INT32 hoffset, moffset;
	UINT8 save;
    INT32 ret1,ret2;
	
	switch(key)
	{
	case V_KEY_UP:
		act = VACT_CURSOR_UP;
		break;
		
	case V_KEY_DOWN:
		act = VACT_CURSOR_DOWN;
		break;		
		
	case V_KEY_EXIT:
	case V_KEY_MENU:
	        act = VACT_CLOSE;
		break;
		
	case V_KEY_ENTER:
		act = VACT_ENTER;
		break;

	default:	
		act = VACT_PASS;
		break;
	}

	return act;
}

static PRESULT aus_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT32 i;
	UINT8	unact;
	CONTAINER* cont = (CONTAINER*)pObj;
	UINT8	id = OSD_GetObjID(pObj);
	CONTAINER* root = &g_win_automatic_upgrade_setting;
	AUTOSCAN_TIME* as_time = sys_data_get_autoscan_upgrade_info();
	
	UINT8 hour, min;
	POBJECT_HEAD Obj =(OBJECT_HEAD*) param2;
	UINT8 save;
	date_time tmp_time;
	
	switch(event)
	{
	case EVN_PRE_OPEN:
		wincom_open_title((POBJECT_HEAD)&g_win_automatic_upgrade_setting,RS_AUTOMATIC_CHANNEL_LIST_UPGRADE,0);
		win_aus_set_Channel_Upgrade_Standby_enable_string(as_time->mode_stby);
		win_aus_set_Channel_Upgrade_Operation_enable_string(as_time->mode_oper);
		set_automatic_channel_upgrade_time(as_time->hour, as_time->min);
        		OSD_SetContainerFocus((CONTAINER*)pObj,1);
		break;
	case EVN_POST_OPEN:
		break;
		
	case EVN_ITEM_PRE_CHANGE:
		if(Obj == (POBJECT_HEAD)&aus_item_con3)
		{
                   get_automatic_channel_upgrade_time_input(&hour, &min);	
	            if(!(check_is_valid_hh_mm(hour, min)))
	            {
	                	win_compopup_init(WIN_POPUP_TYPE_OK);
	    			win_compopup_set_frame(GET_MID_L(500),GET_MID_T(200),500,200);
	    			win_compopup_set_msg_ext(NULL,NULL,RS_MSG_INVALID_INPUT_CONTINUE);
	    			win_compopup_open_ext(&save);
	    			ret = PROC_LOOP;
	            }
              }
		break;
		
	case EVN_PRE_CLOSE:
		if(as_time->mode_stby == 0)
		{
				win_compopup_init(WIN_POPUP_TYPE_OK);
	    			win_compopup_set_frame(GET_MID_L(780),GET_MID_T(200),780,250);
	    			win_compopup_set_msg_ext(NULL,NULL,RS_AUTOMATIC_CHANNEL_UPGRADE_STANDBYOFF_HINT);
	    			win_compopup_open_ext(&save);
		}
		
		get_automatic_channel_upgrade_time_input(&hour, &min);
		if(as_time->hour != hour || as_time->min != min)
		{
			as_time->hour = hour;
			as_time->min = min;
			as_time->running = 0;
		}

		libc_printf("autoscan setting\n");
		
		if(as_time->mode_oper == 1)
		{
			get_local_time(&tmp_time);
			UIGet_recent_autoscan_time(&tmp_time, as_time);
			as_time->mjd = tmp_time.mjd;
			libc_printf("Operation mode setting :\n");
			autoscan_time_debug_printf(as_time, 0);
		}

		sys_data_save(1);
		/* Make OSD not flickering */
		*((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG;	
		break;

	case EVN_POST_CLOSE:
		break;
	}
	return ret;
}
#endif
#endif
